﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace POB
{
	class AddOn : IMenuItem
	{
		public int CostEnergie { get; set; }
		public string Name { get; set; }
		public string Describtion { get; set; }
		private UnitInfo unit;
		private Player player;

		Texture2D itemBackground;
		Texture2D highlightedItemBackground;
		Texture2D menuDescribtionBackground;
		Texture2D menuNothingBackground;
		Texture2D buttonA;

		public AddOn(UnitInfo unit, Player player)
		{
			this.player = player;
			this.unit = unit;

			this.itemBackground = player.World.Content.Load<Texture2D>(@"UI\AddOnInfo");
			this.highlightedItemBackground = player.World.Content.Load<Texture2D>(@"UI\highlightedAddOnInfo");
			this.menuDescribtionBackground = player.World.Content.Load<Texture2D>(@"UI\describtionBackground");
			this.menuNothingBackground = player.World.Content.Load<Texture2D>(@"UI\AddOnNothingSelected");
			this.buttonA = player.World.Content.Load<Texture2D>(@"UI\xboxControllerButtonA");
		}		

		public void DrawIfSelected(SpriteBatch batch, Matrix matrix)
		{
			batch.Begin(Matrix.CreateTranslation(-175f, -155f, 0f) * matrix);

			float middle = menuDescribtionBackground.Width / 2;
			bool canDoAction = player.Energie >= CostEnergie;

			batch.Draw(menuDescribtionBackground, Vector2.Zero, canDoAction ? Color.White : Color.Gray);

			batch.DrawString(player.World.UI36, Name, new Vector2(middle, 30), Color.Black, 0f, player.World.UI36.MeasureString(Name) / 2, 1f, SpriteEffects.None, 0f);
			batch.DrawString(player.World.UI21, Describtion, new Vector2(middle, 60), Color.Black, 0f, new Vector2(player.World.UI21.MeasureString(Describtion).X / 2, 0f), 1f, SpriteEffects.None, 0f);

			batch.DrawString(player.World.UI21, CostEnergie.ToString(), new Vector2(315, 230), Color.Black);

			if(player.Energie >= CostEnergie)
			{
				batch.Draw(buttonA, new Rectangle(95, 265, 35, 35), Color.White);
				batch.DrawString(player.World.UI21, "Launch", new Vector2(140, 295), Color.Black);
			}
			else
			{
				batch.DrawString(player.World.UI21, "No Energie", new Vector2(player.World.UI21.MeasureString("No Slot").X / -2 + middle, 295), Color.Black);
			}

			batch.End();
		}

		public void DrawMenuIcon(SpriteBatch batch, Matrix horizontalMatrix, Matrix rotatedMatrix, bool selected)
		{
			if(selected)
				batch.Begin(horizontalMatrix * Matrix.CreateTranslation(-10f, -10f, 0f));
			else
				batch.Begin(horizontalMatrix);

			bool canDoAction = player.Energie >= CostEnergie;

			batch.Draw(selected ? highlightedItemBackground : itemBackground,
				new Vector2(-85, -85),
				canDoAction ? Color.White : Color.Gray);

			batch.DrawString(player.World.UI36, Name, new Vector2(10f, -40f), Color.Black, 0f, player.World.UI36.MeasureString(Name) / 2, 1f, SpriteEffects.None, 0f);

			batch.DrawString(player.World.UI21, CostEnergie.ToString(), new Vector2(-15f, 42f), Color.Black);


			batch.End();
		}

		public void Selected(CircleMenu parentMenu, InputState input, PlayerIndex playerIndex)
		{
			if(input.IsNewButtonPress(Buttons.A, playerIndex))
			{
				if(player.Energie >= unit.CostEnergie + CostEnergie)
				{
					unit.CreateInstance(this);
					parentMenu.NavigateBack();
				}
			}
			if(input.IsNewButtonPress(Buttons.B, playerIndex))
			{
				parentMenu.NavigateBack();
			}
		}


		public void DrawNothingSelected(SpriteBatch spriteBatch, Matrix matrix)
		{
			spriteBatch.Begin(matrix);

			spriteBatch.Draw(menuNothingBackground, new Vector2(menuNothingBackground.Width - 20, menuNothingBackground.Height) / -2, Color.White);

			spriteBatch.End();
		}

		public void NothingSelected(CircleMenu parentMenu, InputState input, PlayerIndex playerIndex)
		{
			if(input.IsNewButtonPress(Buttons.B, playerIndex))
			{
				parentMenu.NavigateBack();
			}
		}
	}
}
